import cv2 as cv
# from skimage.metrics import structural_similarity
from core.util import timer_elapse, time_str
import numpy as np
import setting as c
from core.log import logger

## 相似性判断
# @timer_elapse
# def ssim_compare(path_image1, path_image2):
#     print(path_image1)
#     # print(path_image2)
#     imageA = cv.imread(path_image1)
#     imageB = cv.imread(path_image2)
#     grayA = cv.cvtColor(imageA, cv.COLOR_BGR2GRAY)
#     grayB = cv.cvtColor(imageB, cv.COLOR_BGR2GRAY)

#     (score, diff) = structural_similarity(grayA, grayB, full=True)
#     # print("SSIM: {}".format(score))
#     return score

# @timer_elapse
# def match_flag(img_path, flags):
#     img = cv.imread(img_path, 0)
#     img2 = img.copy()
    
#     # 获取图像的高度和宽度
#     height, width = img2.shape[:2]
#     logger.info(f"match flag {img_path} {width}x{height}")
#     x = 10000
#     y = 10000
#     shape_out = None
#     ok = False
#     idx = 0
#     for i, flag in enumerate(flags):
#         template = cv.imread(flag.img, 0)
#         w, h = template.shape[::-1]
        
#         img = img2.copy()
#         shape_list = []
#         threshold = 0.80
#         res = cv.matchTemplate(img,template,cv.TM_CCOEFF_NORMED)   
#         min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res) 
#         # print(f"match flag {flag.img} result: {max_val} ")
#         loc = np.where( res >= threshold)


#         for pt in zip(*loc[::-1]):
#             # print(f"pt {pt}")
#             top_left = pt
#             bottom_right = (top_left[0] + w, top_left[1] + h)  
#             shape = (top_left[0],top_left[1],bottom_right[0],bottom_right[1])
#             shape_list.append(shape)
#             new_x = (shape[2]+shape[0])//2
#             if new_x < x :  
#                 shape_out = shape
#                 x = new_x
#                 y = (shape[3]+shape[1])//2
#                 idx = i
                
#         if x <= width or y <= height:
#             ok = True
#             break
    
#     if ok:
#         logger.info(f"match {flags[idx]} ok.  {shape_out} ({x}, {y})")
#         top_left = (shape_out[0], shape_out[1])  # (x, y)
#         bottom_right = (shape_out[2], shape_out[3])  # (x, y)
#         img_color = cv.imread(img_path)
#         ts = time_str()
#         cv.rectangle(img_color, top_left, bottom_right, (0, 0, 255), 2)
        
#         off_x = flags[idx].offset_x
#         off_y = flags[idx].offset_y
#         x += off_x
#         y += off_y
#         top_left = (shape_out[0] + off_x, shape_out[1] + off_y)  # (x, y)
#         bottom_right = (shape_out[2] + off_x, shape_out[3] + off_y)  # (x, y)
#         cv.rectangle(img_color, top_left, bottom_right, (0, 255, 0), 2)
        
#         p = f"{c.flag_match_dir_path}/{flags[idx]}_{ts}.jpg"
#         cv.imwrite(p, img_color)
#         cv.destroyAllWindows()
#         return x, y, True
#     else :
#         print(f"match flag failed! {flags[0]}")
#         img_color = cv.imread(img_path)
#         ts = time_str()
#         p = f"{c.flag_miss_dir_path}/{flags[0]}_{ts}.jpg"
#         cv.imwrite(p, img_color)
#         cv.destroyAllWindows()
#         return None, None, False
    